package cn.ava.org.service.impl;

import cn.ava.basic.service.impl.BaseServiceImpl;
import cn.ava.org.domain.Department;
import cn.ava.org.mapper.DepartmentMapper;
import cn.ava.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @ClassName: DepartmentServiceImpl
 * @Description: 部门管理service实现类
 * @Author: Avavaava
 * @Date: 2022/6/27 19:52
 * @version: 23.53
 *
 */
@Service
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;

    /**
     * @Title: add
     * @Description: 添加并设置上级部门路径
     * @Author: Avavaava
     * @Version: 23.53
     * @Date: 2022/6/27 19:51
     * @param:
     * @return:
     */
    @Override
    @Transactional
    public void add(Department department) {
        departmentMapper.add(department);
        // 调用update方法设置上级部门
        this.update(department);
    }

    /**
     * @Title: update
     * @Description: 修改设置上级部门路径
     * @Author: Avavaava
     * @Version: 23.53
     * @Date: 2022/6/27 19:51
     * @param:
     * @return:
     */
    @Override
    @Transactional
    public void update(Department department) {
        // 创建一个空字符串
        String dirPath = "";
        // 判断上级部门是否为空
        if (department.getParent().getId() == null) {
            // 如果为空,那它自己就是顶级部门,拼接上自己的id即可
            dirPath = "/" + department.getId();
        }else {
            // 如果不是null则有上级部门,先通过上级部门id查询到上级部门对象
            Department parent = departmentMapper.getById(department.getParent().getId());
            // 再拼接上父级部门的dirPath和自己的id
            dirPath = parent.getDirPath() + "/" + department.getId();
        }
        // 最后设置进dirPath中
        department.setDirPath(dirPath);
        departmentMapper.update(department);
    }

    /**
     * @Title: getDeptTree
     * @Description: 查询部门树数据
     * @Author: Avavaava
     * @Version: 23.53
     * @Date: 2022/6/27 19:51
     * @param:
     * @return:
     */
    @Override
    public List<Department> getDeptTree() {
        // 先查询出全部数据
        List<Department> deptAll = departmentMapper.loadAll();
        // 创建一个空数组集合
        ArrayList<Department> depts = new ArrayList<>();
        // 创建一个map集合,将集合数据转换为hashMap
        Map<Long, Department> deptMap = deptAll.stream().collect(Collectors.toMap(Department::getId, item -> item));
        // 遍历集合数据
        for (Department parent : deptAll) {
            // 判断是否是顶级部门
            if (parent.getParentId() == null) {
                // 如果是则放入集合中
                depts.add(parent);
            } else { // 如果不是那就一定是子级部门
                // 根据上级部门id从map中获取上级部门数据
                Department department = deptMap.get(parent.getParentId());
                if (department != null) {
                    // 将当前子级部门数据放入在Children集合对象中
                    department.getChildren().add(parent);
                }
            }
        }
        // 返回部门树数据
        return depts;
    }

}
